package br.com.seosoft.simplecalcfinan.adapters;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import br.com.seosoft.simplecalcfinan.R;
import br.com.seosoft.simplecalcfinan.geral;
import br.com.seosoft.simplecalcfinan.utils.BancosSMS;
import br.com.seosoft.simplecalcfinan.utils.DateFormatSimples;
import br.com.seosoft.simplecalcfinan.utils.IBancos;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdapterListaSms extends BaseAdapter {

	private Activity atividade;
	private IBancos bancos;
	private List<IBancos> listaDebancos = new ArrayList<IBancos>();
	private TextView localBanco = null;
	private TextView valorBanco = null;
	private TextView data = null;
	private LinearLayout layout = null;
	private DateFormatSimples formatarData = new DateFormatSimples();
	private HashSet<Integer> checkedItems;

	public AdapterListaSms(Activity atividade, List<IBancos> listaDebancos) {
		// TODO Auto-generated constructor stub
		this.atividade = atividade;
		this.listaDebancos = listaDebancos;
		this.checkedItems = new HashSet<Integer>();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listaDebancos.size();
	}

	public void removerItens() {
		
		int aux = 0;
		Log.v(geral.LogCat, "Total itens: " + getCount());
		Log.v(geral.LogCat, "Total itens checkeds: " + getCheckedItemCount());
		
		for (int i = 0; i < getCount(); i++) {
			
			

			if (isChecked(i)) {
				aux++;
				listaDebancos.remove(i);
				Log.v(geral.LogCat, "Removido item: " + i);
			}
		}
		
		
		Log.v(geral.LogCat, "Total Remvoer Itens: " + aux);
		notifyDataSetChanged();
	}

	@Override
	public IBancos getItem(int position) {
		// TODO Auto-generated method stub
		return listaDebancos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void setChecked(int pos, boolean checked) {
		if (checked) {
			this.checkedItems.add(Integer.valueOf(pos));
		} else {
			this.checkedItems.remove(Integer.valueOf(pos));
		}

	}

	public boolean isChecked(int pos) {
		return this.checkedItems.contains(Integer.valueOf(pos));
	}

	public void toggleChecked(int pos) {
		final Integer v = Integer.valueOf(pos);
		if (this.checkedItems.contains(v)) {
			this.checkedItems.remove(v);
		} else {
			this.checkedItems.add(v);
		}

	}

	public int getCheckedItemCount() {
		return this.checkedItems.size();
	}

	public Set<Integer> getCheckedItems() {
		return this.checkedItems;
	}
	
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		convertView = atividade.getLayoutInflater().inflate(
				R.layout.adapter_lista_sms, null);

		localBanco = (TextView) convertView.findViewById(R.id.tvLocalBanco);
		valorBanco = (TextView) convertView.findViewById(R.id.tvValorBanco);
		data = (TextView) convertView.findViewById(R.id.tvData);
		layout = (LinearLayout) convertView
				.findViewById(R.id.layoutAdapterListaSms);

		layout.setBackgroundResource(R.drawable.seletor_lista);
		localBanco.setText(listaDebancos.get(position).getDescricao());
		valorBanco.setText(listaDebancos.get(position).getValor().toString());
		data.setText(listaDebancos.get(position).getData().toLocaleString());
		return convertView;
	}

}
